foreach y in ca_lim_imInc {

	local category "cohold_day cohold_day_frequent cohold_day_intense cohold_day_costly cohold_spell_cat cohold_runup_cat"
	gen subcategory = ""
	gen mean = .
	gen lqt = .
	gen uqt = .
	gen n = _n
	local i = 1

	foreach var in `category' {
		if "`var'" == "cohold_day" | "`var'" == "cohold_day_frequent" | "`var'" == "cohold_day_intense" | "`var'" == "cohold_day_costly" {
			local min 0
			local max 1
		} 
		else if "`var'" == "cohold_spell_cat" {
		local min 0
		local max 5
		}
		else if "`var'" == "cohold_runup_cat" {
		local min 0
		local max 2
		}
		
		 forvalues j = `min'/`max' { 
			replace subcategory = "`var'`j'" if n == `i'    
			ci means `y' if `var' == `j' & coholder==1, level(90)
			gen m = `r(mean)'
			gen l = `r(lb)'
			gen u = `r(ub)'
			replace mean = m if subcategory == "`var'`j'"
			replace lqt  = l if subcategory == "`var'`j'"
			replace uqt  = u if subcategory == "`var'`j'"
			local i = `i' + 1
			drop m l u
		 }
		local i = `i' + 1	 
	}

	sort n
	egen min = min(lqt*10)
	egen max = max(uqt*10)

	twoway ///
	rcap uqt lqt n if subcategory != "", lstyle(ci) ||  ///
	 scatter mean n if subcategory != "", ///
	 title("Overdraft limit relative to average monthly income", place(left) size(large)) ///
	 ytitle("") ///
	 xtitle("")  ///
	 mcolor(blue) lcolor(blue) mstyle(p1) ///
	 xline(3 6 9 12 19, lpattern(dot)) ///
	 ylabel(, labsize(medium) angle(0) format(%9.1fc)) /// 
	 xlabel(0.7 `""Coholder:" "No cohold day""' 2 "Cohold day" ///
		 3.7 `""Frequent coholder:" "No cohold day""' 5 "Cohold day" ///
		 6.7  `""Intense coholder:" "No cohold day""' 8 "Cohold day" ///
		 9.7  `""Costly coholder:" "No cohold day""' 11 "Cohold day" ///
		 12.7 `""Coholder:" "None""' 14 "Start" 15 "First 1/3" 16 "Intermediate 1/3" 17 "Last 1/3" 18 "End" ///
		 19.7 `""Coholder:" "Cohold day""' 21 "Just stopped" 22 "About to start", ///
		labsize(medsmall) angle(90))  ///
	 xscale(range(0.25 22.75)) ///
	 legend(off)	 
}
drop subcategory mean lqt uqt n 
drop min max  	 

graph export "Figures/CohExpl_CreditAccess_groups.pdf", replace
graph export "Figures/CohExpl_CreditAccess_groups.png", replace


*T-test related to graph: 
eststo a: quietly estpost summarize ca_lim_imInc if coholder==1 & cohold_day==0, detail
eststo b: quietly estpost summarize ca_lim_imInc if coholder==1 & cohold_day==1, detail
eststo diff: quietly estpost ttest ca_lim_imInc if coholder==1 & cohold_day==1| coholder==1 & cohold_day==0, by(cohold_day)

esttab a b diff using "Tables/Coh_expl_CreditAccess_ttest.tex", replace ///
cells("mean(label(Mean) pattern(1 1 0) fmt(%15.2fc)) p25(label(P25) pattern(1 1 0) fmt(%15.2fc)) p50(label(P50) pattern(1 1 0) fmt(%15.2fc)) p75(label(P75) pattern(1 1 0) fmt(%15.2fc)) p90(label(P90) pattern(1 1 0) fmt(%15.2fc)) b(label(Diff.) star pattern(0 0 1) fmt(%15.2fc)) se(label(SE) par pattern(0 0 1) fmt(%15.2fc))") ///
mtitle("\shortstack{Coholder: \\ No cohold day}" "\shortstack{Coholder:\\ Cohold day}" "") ///
booktabs noobs nonumbers ///
starlevels(* 0.10 ** 0.05 *** 0.01) ///
rename( ///
ca_lim_imInc "Overdraft limit") ///
substitute(? \) 

********************************************************************************

***Degree of current account dynamics

*Nr occurrences of limit change (either increase or decrease) that is followed
*by a change in the opposite direction

*1. Daily change in balance (already calculated in variables generation file)

*2. Direction of change in balance
gen ca_lim_incr = 0 if ca_lim !=.
replace ca_lim_incr = 1 if ca_lim_dchange > 0 & ca_lim_dchange != . & ca_lim !=.
gen ca_lim_decr = 0 if ca_lim !=.
replace ca_lim_decr = 1 if ca_lim_dchange < 0 & ca_lim_dchange != . & ca_lim !=.

*3. Total number of i) increases ii) decreases in balance
bysort ind_id: egen total_ca_lim_incr = total(ca_lim_incr)
bysort ind_id: egen total_ca_lim_decr = total(ca_lim_decr)

*4. Total nominal i) increase ii) decrease in balance
bysort ind_id: egen nom_ca_lim_incr = total(ca_lim_dchange) if ca_lim_incr==1
bysort ind_id: egen max_nom_ca_lim_incr = max(nom_ca_lim_incr)
replace nom_ca_lim_incr = max_nom_ca_lim_incr
drop max_nom_ca_lim_incr
bysort ind_id: egen nom_ca_lim_decr = total(ca_lim_dchange) if ca_lim_decr==1
bysort ind_id: egen max_nom_ca_lim_decr = max(nom_ca_lim_decr)
replace nom_ca_lim_decr = max_nom_ca_lim_decr
drop max_nom_ca_lim_decr

*5. Make positive out of negative amount and divide by 1000
replace nom_ca_lim_decr = abs(nom_ca_lim_decr)
replace nom_ca_lim_incr = nom_ca_lim_incr/1000
replace nom_ca_lim_decr = nom_ca_lim_decr/1000

*6. Cut large values and divide by 1000
foreach var in nom_ca_lim_incr nom_ca_lim_decr {
gen `var'_cat = `var'
replace `var'_cat = 2000 if `var' >=2000 & `var'!=.
}


*7. Plot the graph for number of increases vs. decreases
summ total_ca_lim_incr total_ca_lim_decr if edate_id == 1335 & random <2700 //target 3000 observations
foreach y in total_ca_lim_incr {
foreach x in total_ca_lim_decr {
pwcorr `y' `x' if edate_id==1335 & random <2700,sig
local cor: display %5.3f r(rho) 
reg `y' `x' if edate_id==1335 & random <2700
local t: di _b[`x']/_se[`x']
if abs(_b[`x']/_se[`x']) > 2.58 {
 local star "***" 
 }
if abs(_b[`x']/_se[`x']) > 1.96 & _b[`x']/_se[`x'] <= 2.58 {
 local star "**" 
 }
 else if abs(_b[`x']/_se[`x']) > 1.64 & _b[`x']/_se[`x'] <= 1.96 {
 local star "*" 
 }
}
}
summarize total_ca_lim_incr if edate_id == 1335 & random <2700, detail
local mediany: di %5.3f r(p50)
summariz total_ca_lim_decr if edate_id == 1335 & random <2700, detail
local medianx: di %5.3f r(p50)

scatter ///
total_ca_lim_incr total_ca_lim_decr if edate_id == 1335 & random <2700, ///
mcolor(blue) || ///
qfit total_ca_lim_incr total_ca_lim_decr if edate_id == 1335 & random <2700, ///
lcolor(blue) ///
yline(`mediany', lpattern(dot)) ///
xline(`medianx', lpattern(dot)) ///
title("Total occurrences of current account limit increases", place(left) size(large)) ///
ytitle("") ///
xtitle("Total occurrences of current account limit decreases", size(large)) ///
ylabel(, labsize(medium) angle(0)) ///
xlabel(, labsize(medium)) ///
subtitle("Correlation coefficient = `cor'`star'", ///
size(medium) position(5) ring(0)) ///
legend(off)

graph export "Figures/CohExp_CALimit_scatter1.png", width(1800) height(1300) replace
graph export "Figures/CohExp_CALimit_scatter1.pdf", replace



*8. Plot the graphs for nominal increases vs decreases
*summ nom_ca_lim_incr_cat if edate_id==1335 & random < 3500 //3,077

foreach y in nom_ca_lim_incr_cat {
foreach x in nom_ca_lim_decr_cat {
pwcorr `y' `x' if edate_id==1335 & random <3500,sig
local cor: display %5.3f r(rho) 
reg `y' `x' if edate_id==1335 & random <3500
local t: di _b[`x']/_se[`x']
if abs(_b[`x']/_se[`x']) > 2.58 {
 local star "***" 
 }
if abs(_b[`x']/_se[`x']) > 1.96 & _b[`x']/_se[`x'] <= 2.58 {
 local star "**" 
 }
 else if abs(_b[`x']/_se[`x']) > 1.64 & _b[`x']/_se[`x'] <= 1.96 {
 local star "*" 
 }
}
}
summarize nom_ca_lim_incr_cat if edate_id == 1335 & random <3500, detail
local mediany: di %5.3f r(p50)
summariz nom_ca_lim_decr_cat if edate_id == 1335 & random <3500, detail
local medianx: di %5.3f r(p50)

summ nom_ca_lim_incr_cat nom_ca_lim_decr_cat if edate_id == 1335 & random <3500
scatter nom_ca_lim_incr_cat nom_ca_lim_decr_cat if edate_id == 1335 & random <3500, ///
mcolor(blue) || ///
qfit nom_ca_lim_incr_cat nom_ca_lim_decr_cat if edate_id == 1335 & random <3500, ///
lcolor(blue) ///
yline(`mediany', lpattern(dot)) ///
xline(`medianx', lpattern(dot)) ///
title("Total nominal current account limit increase (in 1,000)", place(left) size(large)) ///
ytitle("") ///
xtitle("Total nominal current account limit decrease (in 1,000)", size(large)) ///
ylabel(, labsize(medium) angle(0)) ///
xlabel(, labsize(medium)) ///
subtitle("Correlation coefficient = `cor'`star'", ///
size(medium) position(5) ring(0)) ///
legend(off) 
graph export "Figures/CohExp_CALimit_scatter2.png", width(1800) height(1300) replace
graph export "Figures/CohExp_CALimit_scatter2.pdf", replace


*9. Calculate switch share
gen ca_lim_act = ca_lim_incr + ca_lim_decr
bysort ind_id: egen total_ca_lim_act = total(ca_lim_act) //number of total movements
gen ca_lim_change_switch = 1 if ca_lim_incr==1
replace ca_lim_change_switch = 2 if ca_lim_decr==1 
tsset new_ind_id edate
bysort ind_id: carryforward ca_lim_change_switch, gen(ca_lim_change_switch_1)
tsset new_ind_id edate
gen ca_lim_change_switch_1_l = l.ca_lim_change_switch_1

gen ca_lim_switch = 1 if ca_lim_change_switch != ca_lim_change_switch_1_l & ca_lim_change_switch !=.
replace ca_lim_switch = 0 if ca_lim_change_switch == ca_lim_change_switch_1_l
bysort ind_id: egen total_ca_lim_switch = total(ca_lim_switch)

gen ca_lim_switchshare = total_ca_lim_switch / total_ca_lim_act

*10. Report key variables as summary statistics, for all individuals
eststo a: quietly estpost summarize ///
total_ca_lim_incr total_ca_lim_decr ///
nom_ca_lim_incr nom_ca_lim_decr ///
total_ca_lim_switch total_ca_lim_act ca_lim_switchshare ///
if edate_id==1335 & total_ca_lim_act !=0 & total_ca_lim_switch!=0, detail

esttab a using "Tables/Coh_expl_CreditAccess_switch.tex", replace ///
cells("mean(label(Mean) fmt(%15.0fc %15.0fc %15.0fc %15.0fc %15.0fc %15.0fc 1)) p25(label(P25) fmt(%15.0fc %15.0fc %15.0fc %15.0fc %15.0fc %15.0fc 1)) p50(label(P50) fmt(%15.0fc %15.0fc %15.0fc %15.0fc %15.0fc %15.0fc 1)) p75(label(P75) fmt(%15.0fc %15.0fc %15.0fc %15.0fc %15.0fc %15.0fc 1)) p90(label(P90) fmt(%15.0fc %15.0fc %15.0fc %15.0fc %15.0fc %15.0fc 1)) count(label(\#Obs.) fmt(%15.0fc))") ///
nomtitle ///
booktabs noobs nonumbers ///
starlevels(* 0.10 ** 0.05 *** 0.01) ///
rename( ///
total_ca_lim_incr "Total nr limit increases" total_ca_lim_decr "Total nr limit decreases" ///
nom_ca_lim_incr "Total limit increase (1,000)" nom_ca_lim_decr "Total limit decrease (1,000)" ///
total_ca_lim_switch "Switches in limit movements" ///
total_ca_lim_act "Total limit movements" ///
ca_lim_switchshare "Switches rel! to limit movements" ///
) ///
substitute(? \ ! .)

*11. Report key variables as summary statistics, for non-coholders vs coholders
eststo a: quietly estpost summarize ///
total_ca_lim_incr total_ca_lim_decr ///
nom_ca_lim_incr nom_ca_lim_decr ///
total_ca_lim_switch total_ca_lim_act ca_lim_switchshare ///
if coholder==0 & edate_id==1335 & ///
total_ca_lim_act !=0 & total_ca_lim_switch!=0, detail

eststo b: quietly estpost summarize ///
total_ca_lim_incr total_ca_lim_decr ///
nom_ca_lim_incr nom_ca_lim_decr ///
total_ca_lim_switch total_ca_lim_act ca_lim_switchshare ///
if coholder==1 & edate_id==1335 & ///
total_ca_lim_act !=0 & total_ca_lim_switch!=0, detail

eststo diff: quietly estpost ttest ///
total_ca_lim_incr total_ca_lim_decr ///
nom_ca_lim_incr nom_ca_lim_decr ///
total_ca_lim_switch total_ca_lim_act ca_lim_switchshare ///
if ///
coholder==0 & edate_id==1335 & ///
total_ca_lim_act !=0 & total_ca_lim_switch!=0 | ///
coholder==1 & edate_id==1335 & ///
total_ca_lim_act !=0 & total_ca_lim_switch!=0, ///
by(coholder)

esttab a b diff using "Tables/Coh_expl_CreditAccess_switch2.tex", replace ///
cells("mean(label(Mean) pattern(1 1 0) fmt(%15.0fc %15.0fc %15.0fc %15.0fc %15.0fc %15.0fc 1)) p25(label(P25) pattern(1 1 0) fmt(%15.0fc %15.0fc %15.0fc %15.0fc %15.0fc %15.0fc 1)) p50(label(P50) pattern(1 1 0) fmt(%15.0fc %15.0fc %15.0fc %15.0fc %15.0fc %15.0fc 1)) p75(label(P75) pattern(1 1 0) fmt(%15.0fc %15.0fc %15.0fc %15.0fc %15.0fc %15.0fc 1)) p90(label(P90) pattern(1 1 0) fmt(%15.0fc %15.0fc %15.0fc %15.0fc %15.0fc %15.0fc 1)) b(label(Diff.) star pattern(0 0 1) fmt(%15.0fc %15.0fc %15.0fc %15.0fc %15.0fc %15.0fc 1)) se(label(SE) par pattern(0 0 1) fmt(%15.0fc %15.0fc %15.0fc %15.0fc %15.0fc %15.0fc 1))") ///
mtitle("No coholder" "Coholder" "") ///
booktabs noobs nonumbers ///
starlevels(* 0.10 ** 0.05 *** 0.01) ///
rename( ///
total_ca_lim_incr "Total nr limit increases" total_ca_lim_decr "Total nr limit decreases" ///
nom_ca_lim_incr "Maximal limit increase (1000)" nom_ca_lim_decr "Maximal limit decrease (1000)" ///
total_ca_lim_switch "Switches in limit movements" ///
total_ca_lim_act "Total limit movements" ///
ca_lim_switchshare "Switches rel! to limit movements" ///
) ///
substitute(? \ ! .)


*12. Report key variables as summary statistics, for 
*those who never have overdrafts (yet have limit changes)
bysort ind_id: egen max_od_amount = max(od_amount)
gen od_pers = 1 if max_od_amount != 0 & max_od_amount !=.
replace od_pers = 0 if max_od_amount == 0

eststo a: quietly estpost summarize ///
total_ca_lim_incr total_ca_lim_decr ///
nom_ca_lim_incr nom_ca_lim_decr ///
total_ca_lim_switch total_ca_lim_act ca_lim_switchshare ///
if od_pers==0 & edate_id==1335 & ///
total_ca_lim_act !=0 & total_ca_lim_switch!=0, detail

eststo b: quietly estpost summarize ///
total_ca_lim_incr total_ca_lim_decr ///
nom_ca_lim_incr nom_ca_lim_decr ///
total_ca_lim_switch total_ca_lim_act ca_lim_switchshare ///
if od_pers==1 & edate_id==1335 & ///
total_ca_lim_act !=0 & total_ca_lim_switch!=0, detail

eststo diff: quietly estpost ttest ///
total_ca_lim_incr total_ca_lim_decr ///
nom_ca_lim_incr nom_ca_lim_decr ///
total_ca_lim_switch total_ca_lim_act ca_lim_switchshare ///
if ///
od_pers==0 & edate_id==1335 & ///
total_ca_lim_act !=0 & total_ca_lim_switch!=0 | ///
od_pers==1 & edate_id==1335 & ///
total_ca_lim_act !=0 & total_ca_lim_switch!=0, ///
by(od_pers)

esttab a b diff using "Tables/Coh_expl_CreditAccess_switch3.tex", replace ///
cells("mean(label(Mean) pattern(1 1 0) fmt(%15.0fc %15.0fc %15.0fc %15.0fc %15.0fc %15.0fc 1)) p25(label(P25) pattern(1 1 0) fmt(%15.0fc %15.0fc %15.0fc %15.0fc %15.0fc %15.0fc 1)) p50(label(P50) pattern(1 1 0) fmt(%15.0fc %15.0fc %15.0fc %15.0fc %15.0fc %15.0fc 1)) p75(label(P75) pattern(1 1 0) fmt(%15.0fc %15.0fc %15.0fc %15.0fc %15.0fc %15.0fc 1)) p90(label(P90) pattern(1 1 0) fmt(%15.0fc %15.0fc %15.0fc %15.0fc %15.0fc %15.0fc 1)) b(label(Diff.) star pattern(0 0 1) fmt(%15.0fc %15.0fc %15.0fc %15.0fc %15.0fc %15.0fc 1)) se(label(SE) par pattern(0 0 1) fmt(%15.0fc %15.0fc %15.0fc %15.0fc %15.0fc %15.0fc 1))") ///
mtitle("No overdraft person" "Overdraft person" "") ///
booktabs noobs nonumbers ///
starlevels(* 0.10 ** 0.05 *** 0.01) ///
rename( ///
total_ca_lim_incr "Total nr limit increases" total_ca_lim_decr "Total nr limit decreases" ///
nom_ca_lim_incr "Maximal limit increase (1000)" nom_ca_lim_decr "Maximal limit decrease (1000)" ///
total_ca_lim_switch "Switches in limit movements" ///
total_ca_lim_act "Total limit movements" ///
ca_lim_switchshare "Switches rel! to limit movements" ///
) ///
substitute(? \ ! .)



********************************************************************************

*Current account limit over the calendar month
foreach y in ca_lim {

	local category "ind_emy_seq"
	gen subcategory = ""
	gen mean = .
	gen lqt = .
	gen uqt = .
	gen n = _n
	local i = 1

		foreach var in `category' {
		if "`var'" == "ind_emy_seq" {
			local min 1
			local max 31
		} 
		
		 forvalues j = `min'/`max' { 
			replace subcategory = "`var'`j'" if n == `i'    
			ci means `y' if `var' == `j' & coholder==1, level(90)
			gen m = `r(mean)'
			gen l = `r(lb)'
			gen u = `r(ub)'
			replace mean = m if subcategory == "`var'`j'"
			replace lqt  = l if subcategory == "`var'`j'"
			replace uqt  = u if subcategory == "`var'`j'"
			local i = `i' + 1
			drop m l u
		 }
		local i = `i' + 1	 
	}

	sort n
	egen min = min(lqt*10)
	egen max = max(uqt*10)

	twoway ///
	rcap uqt lqt n if subcategory != "", lstyle(ci) ||  ///
	 scatter mean n if subcategory != "", ///
	 mcolor(blue) mstyle(p1) ///
	 title("Current account limit", place(left) size(large)) ///
	 ytitle("") ///
	 xtitle("th day in month", size(large)) ///
	 xlabel(0 5 10 15 20 25 30, labsize(medium) angle(0))  ///
	 ylabel(, labsize(medium) angle(0) format(%9.0fc)) /// 
	 xscale(range(0.25 31.75)) ///
	 legend(off)
	 
drop subcategory mean lqt uqt n 
drop min max  	 
}
graph export "Figures/CohExpl_CreditAccess_month.pdf", replace
graph export "Figures/CohExpl_CreditAccess_month.png", replace


*Average duration of coholding for coholders
foreach y in cohold_length {

	local category "emy_id"
	gen subcategory = ""
	gen mean = .
	gen lqt = .
	gen uqt = .
	gen n = _n
	local i = 1

		foreach var in `category' {
		if "`var'" == "emy_id" {
			local min 1
			local max 29
		} 
		
		 forvalues j = `min'/`max' { 
			replace subcategory = "`var'`j'" if n == `i'    
			ci means `y' if `var' == `j' & coholder==1 & ind_spell_obs==1 & cohold_day==1, level(90) //only for coholder and activate individual x spell
			gen m = `r(mean)'
			gen l = `r(lb)'
			gen u = `r(ub)'
			replace mean = m if subcategory == "`var'`j'"
			replace lqt  = l if subcategory == "`var'`j'"
			replace uqt  = u if subcategory == "`var'`j'"
			local i = `i' + 1
			drop m l u
		 }
		local i = `i' + 1	 
	}

	sort n
	egen min = min(lqt*10)
	egen max = max(uqt*10)

	twoway ///
	rcap uqt lqt n if subcategory != "", lstyle(ci) ||  ///
	 scatter mean n if subcategory != "", ///
	 mcolor(blue) mstyle(p1) ///
	 title("Average duration of coholding (days)", place(left) size(large)) ///
	 ytitle("") ///
	 xtitle("") ///
	 ylabel(, labsize(medium) angle(0) format(%9.0fc)) /// 
	 xlabel(1 "September 2014" 15 "November 2015" 29 "January 2017", angle(0) labsize(medium))  ///
	 xscale(range(0.25 29.75)) ///
	 graphregion(margin(large)) ///
	 legend(off)  	 
}
drop subcategory mean lqt uqt n 
drop min max

graph export "Figures/CohExpl_CreditAccess_DurCoholding.pdf", replace
graph export "Figures/CohExpl_CreditAccess_DurCoholding.png", replace

